package com.anti.jdbc.support.mapper;

import com.anti.commom.core.utils.ClassUtils;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-13
 */

public class BeanRowMapper<T> implements RowMapper<T> {
    private final ResultSetToBeanProcess resultSetToBeanProcess = new ResultSetToBeanProcess();
    private Class<T> clz = null;
    private boolean isPrimitive = false;

    public BeanRowMapper(Class<T> clz) {
        this.clz = clz;
        this.isPrimitive = ClassUtils.isPrimitiveOrWrapper(clz);
    }

    public T mapRow(ResultSet rs, int pos) throws SQLException {
        return this.isPrimitive ? this.resultSetToBeanProcess.toPrimitive(rs, this.clz) : this.resultSetToBeanProcess.toBean(rs, this.clz);
    }
}

